package com.sam.im.heiqun.uis.activities;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sam.im.heiqun.utils.ImageCompressUtil;
import com.sam.im.heiqun.widgets.imagepicker.PhotoPickerActivity;
import com.sam.im.heiqun.widgets.imagepicker.SelectModel;
import com.sam.im.heiqun.widgets.imagepicker.intent.PhotoPickerIntent;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.RequestRefereeEntity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.FileSaveUtil;
import com.sam.im.heiqun.utils.HttpAssist;
import com.sam.im.heiqun.utils.ImageCropViewActivity;
import com.sam.im.heiqun.utils.PictureUtil;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.soundcloud.android.crop.Crop;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.data.PrefsUtils;
import com.yuyh.library.utils.log.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.soundcloud.android.crop.Crop.REQUEST_CROP;

/**
 * 注册
 */
public class RegisterActivity extends BaseSwipeBackActivity implements CompoundButton.OnCheckedChangeListener, Handler.Callback {

    public static final int REQUEST_COUNTRY = 1000;
    private static final int HEAD_IMG_URL = 1001;
    public static final int TIME_SUB = 1002;
    private static final String TAG = RegisterActivity.class.getSimpleName();

    @BindView(R.id.nick)
    EditText nick;
    @BindView(R.id.get_sign)
    TextView getSign;
    @BindView(R.id.auth_code)
    EditText authCode;
    @BindView(R.id.phone_number)
    EditText phoneNumber;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.is_agree)
    CheckBox isAgree;
    @BindView(R.id.registration_protocol)
    TextView registrationProtocol;
    @BindView(R.id.register)
    Button register;
    @BindView(R.id.activity_register)
    LinearLayout activityRegister;
    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.head_img)
    ImageView headImg;

    @BindView(R.id.referrer)
    EditText referrer;
    @BindView(R.id.country)
    TextView country;
    @BindView(R.id.country_code)
    TextView countryCode;

    PGService mPgService;
    private boolean isAgreeb = false;
    private String countryStr;
    private String mobileStr = "+86";
    private Handler handler = new Handler(this);
    private File photoFile;
    private String userId;
    private boolean haveImg;
    private int CAMERA_JAVA_REQUEST_CODE = 5;
    private static final int SDK_PERMISSION_REQUEST = 127;
    private String permissionInfo;
    private AlertDialog affirmDialog;

    private int timeNum = 60;
    private String headImgUrl = "";
    private Uri imageUri;
    private String choesimgpath;

    private String authInfo;//获取到的短信验证码
    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case HEAD_IMG_URL:
                headImgUrl = ((String) message.obj);
                String nickStr = nick.getText().toString().trim();
                String phoneStr = phoneNumber.getText().toString().trim();
                String authCodeStr = authCode.getText().toString().trim();
                String passwordStr = pwd.getText().toString().trim();
                String recommandUserId = referrer.getText().toString().trim();
                if (nickStr.length() > 10) {
                    showToast(getResources().getString(R.string.name_out_10));
                    break;
                }
                if (passwordStr.length() < 6){
                    showToast(getResources().getString(R.string.password_least_six));
                    break;
                }
                if (passwordStr.length() > 15){
                    showToast(getResources().getString(R.string.password_out_15));
                    break;
                }
                register(headImgUrl, mobileStr + phoneStr, passwordStr, nickStr, authCodeStr, recommandUserId);
                break;
            case TIME_SUB:
                timeNum--;
                if (timeNum > 0) {
                    getSign.setText(timeNum + "s");
                    handler.sendEmptyMessageDelayed(TIME_SUB, 1000);
                } else {
                    getSign.setText(R.string.get_auth_code);
                    getSign.setTextColor(getResources().getColor(R.color.colorAccent));
                    timeNum = 60;
                    getSign.setEnabled(true);
                }
                break;
        }
        return false;
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.register);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        countryStr = getString(R.string.chain);
        mPgService = PGService.getInstance();
        isAgree.setOnCheckedChangeListener(this);
       // register.setEnabled(false);

    }

    /**
     * 点击事件处理
     */
    @OnClick({R.id.registration_protocol, R.id.pre_v_back, R.id.get_sign, R.id.country, R.id.head_img, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.registration_protocol:
//               ToolsUtils.showToast(RegisterActivity.this,"暂无注册协议");
                startActivity(RegisterAgreementActivity.class);
                break;
            case R.id.register: {
                judgeDataEntire();
                break;
            }
            case R.id.pre_v_back: {
                scrollToFinishActivity();
                break;
            }

            case R.id.get_sign: {
                String phoneNumberStr = phoneNumber.getText().toString().trim();
                boolean isMobileNO = ToolsUtils.isMobile(phoneNumberStr);

                if (phoneNumberStr.length() > 0) {
                    if (!isMobileNO){
                        Log.i("info","phone=="+phoneNumberStr);
                        showToast(getResources().getString(R.string.error_phone));
                        return;
                    }
                    getSign.setEnabled(false);
                    getSign.setText(timeNum + "s");
                    getSign.setTextColor(getResources().getColor(R.color.middle_gray_2));
                    handler.sendEmptyMessageDelayed(TIME_SUB, 1000);
                    Log.i("info","==="+mobileStr+"==="+phoneNumberStr);
                    getValidateNumCheck(mobileStr + phoneNumberStr);
                }
                break;
            }
            case R.id.country:
                startActivityForResult(CountrySelectActivity.class, REQUEST_COUNTRY);
                break;
            case R.id.head_img:
                getPersimmions();
                break;
            case R.id.affirm:
                affirmDialog.dismiss();
                uploadImage(photoFile);
                break;
        }
    }

    /**
     * 判断内容是否符合   提交
     */
    private void judgeDataEntire() {
        if (!ToolsUtils.currentNetState(this)) {
            return;
        }
        String nickStr = nick.getText().toString().trim();
        String phoneStr = phoneNumber.getText().toString().trim();
        String authCodeStr = authCode.getText().toString().trim();
        String passwordStr = pwd.getText().toString().trim();

        if ("".equals(nickStr)) {
            showToast(getResources().getString(R.string.please_import_nick));
            return;
        } else if ("".equals(phoneStr)) {
            showToast(getResources().getString(R.string.please_import_correct_phone));
            return;
        } else if (authCodeStr.equals("")) {
            showToast(getResources().getString(R.string.please_import_auth_code));
            return;
        } else if ("".equals(passwordStr)) {
            showToast(getResources().getString(R.string.please_import_psd));
            return;
        }
//        if (authCodeStr.length() < 8) {
//            showToast(getResources().getString(R.string.yzm_out_8));
//            return;
//        }
        if (passwordStr.length() < 6) {
            showToast(getResources().getString(R.string.password_least_six));
            return;
        }
        if (passwordStr.length() > 15) {
            showToast(getResources().getString(R.string.password_out_15));
            return;
        }
        if (nickStr.length() > 10) {
            showToast(getResources().getString(R.string.name_out_10));
            return;
        }
        // 这里判断是否有头像, 按照需求来
//        else if (!haveImg) {
//            showToast(getResources().getString(R.string.please_upload_head_img));
//            return;
//        }

        if (isAgreeb) {
            if (!referrer.getText().toString().trim().equals("")) {
                showAffirmDialog();
            } else {
                uploadImage(photoFile);
            }
        }else {
            ToolsUtils.showToast(RegisterActivity.this,getResources().getString(R.string.p_agree_regist));
        }
    }

    /**
     * 调用 Activity 返回值处理
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_COUNTRY:
                    countryStr = data.getStringExtra("country");
                    mobileStr = data.getStringExtra("mobileCode");

                    country.setText(countryStr);
                    countryCode.setText(mobileStr);
                    break;
                case 66:    // 相机
                {
                    String path = FileSaveUtil.getPath(this, imageUri);
                    try {
                        Bitmap bitmap = PictureUtil.compressSizeImage(path);
                        bitmap = PictureUtil.reviewPicRotate(bitmap, path);
                        photoFile = PictureUtil.bitmapToFileJPG(bitmap, path);
                        GlideUtils.loadHeadCircularImage(RegisterActivity.this, path, headImg);
                        haveImg = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                }
                case 77:    // 图库
                    Uri uri77 = data.getData();
                    try {
                        Bitmap bitmap = PictureUtil.getBitmapFormUri(this, uri77);
                        String path = FileSaveUtil.getPath(getApplicationContext(), uri77);
                        GlideUtils.loadHeadCircularImage(this, path, headImg);

                        photoFile = PictureUtil.bitmapToFileJPG(bitmap, path);
                        haveImg = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    break;

                case 3333:    // 图库选择返回
                {
//                    Uri uri = data.getData();
//                    String path = FileSaveUtil.getPath(getApplicationContext(), uri);
                    try {
                        GlideUtils.loadHeadCircularImage(this, choesimgpath, headImg);
//                        Bitmap bitmap = PictureUtil.getBitmapFormUri(this, uri);
//                        headImgFile = bitmapToFile(bitmap);
                        photoFile = new File(choesimgpath);
                        haveImg = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                }
                //新换的图片裁剪
                case REQUEST_CROP:

                    LogUtils.d("图片裁剪uri11:"+Crop.getOutput(data));
                    LogUtils.d("图片裁剪uri22:"+newUri);
                    try {
                   //     GlideUtils.loadHeadCircularImage(this, choesimgpath, headImg);
                        headImg.setImageURI(Crop.getOutput(data));

//                        Bitmap bitmap = PictureUtil.getBitmapFormUri(this, uri);
//                        headImgFile = bitmapToFile(bitmap);
                         photoFile = new File(choesimgpath);
                       // photoFile=ToolsUtils.getFileByUri(newUri,this);
                        haveImg = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                case CHOSE_IMG :
                    ArrayList<String> stringArrayListExtra = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT);
                    if (stringArrayListExtra.size()>0){
                        choesimgpath = stringArrayListExtra.get(0);
                        Log.i("info","返回的路径s=="+ choesimgpath);
                        final File file = new File(choesimgpath);
                        if (file.exists()){
                            Uri uri = Uri.fromFile(file);
//                            ToolsUtils.cropImageUri(this,uri,600,600,3333);
                             newUri  =  Uri.fromFile(new File( getCacheDir(), "cropped"+System.currentTimeMillis()));
                           // Uri destination =;

                            Crop.of(uri, newUri).asSquare().start(this);

                         //   ImageCropViewActivity.start(this,choesimgpath,600,600,3333);
                        }else {
                            ToolsUtils.showToast(RegisterActivity.this,getResources().getString(R.string.file_no));
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    }
    private Uri newUri;

  /*  *//**
     * 获取验证码
     *//*
    private void getValidateNum(String thephoneNumber) {
        Log.i("info","==="+thephoneNumber);
        mPgService.getValidateNum(thephoneNumber).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            public void onNext(ValidateEntivity s) {
                // 这里没做验证码的成员变量存储。也就是不做本地验证码判断
                //authCode.setText(s.getInfo().toString());
                // 查询注册账号是否有推荐人
                queryReferee();
            }

            @Override
            protected void onResultError(ApiException ex) {
                if (ex.getCode()==-1){
                    try {

                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(ex.getDisplayMessage());
                        com.alibaba.fastjson.JSONObject data = object.getJSONObject("data");

                        showToast(data.getString("info"));
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else {
                    showToast(getResources().getString(R.string.net_visit_exception));
                }
            }
        });
    }*/

    /**
     * 查询推荐人
     */
    private void queryReferee() {
        String mobile = phoneNumber.getText().toString().trim();
        mPgService.getrefer(mobile).subscribe(new AbsAPICallback<RequestRefereeEntity>() {
            @Override
            protected void onResultError(ApiException ex) {
                Log.e(TAG, "onResultError: 获取推荐人信息失败---");
            }

            @Override
            public void onNext(RequestRefereeEntity requestRefereeEntity) {
                int refereeId = requestRefereeEntity.getInfo().getId();
                referrer.setText(refereeId + "");
                // 后台要求 不让修改
                referrer.setEnabled(false);
            }
        });
    }


    /**
     * 获取验证码
     */
    private void getValidateNumCheck(String phoneNumber) {
        mPgService.getValidateNumCheck(phoneNumber).subscribe(new AbsAPICallback<ValidateEntivity>() {

            @Override
            public void onNext(ValidateEntivity s) {//成功之后才开始倒计时
                getSign.setText(timeNum + "s");
                getSign.setTextColor(getResources().getColor(R.color.middle_gray_2));
                handler.sendEmptyMessageDelayed(TIME_SUB, 1000);
                authInfo = s.getInfo();
                authCode.setText(s.getInfo());
            }

            @Override
            protected void onResultError(ApiException ex) {
                getSign.setEnabled(true);
                if (ex.getCode() == 2) {
//                    showToast(getResources().getString(R.string.phoneiserror));
                    try {

                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(ex.getDisplayMessage());
                        com.alibaba.fastjson.JSONObject data = object.getJSONObject("data");

                        showToast(data.getString("info"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showToast(getResources().getString(R.string.net_visit_exception));
                }
            }
        });
    }



    /**
     * 注册接口
     */
    private void register(String headUrl, final String mobile, final String pwd, String name, String validateNum, String recommandUserId) {
        String authStr = authCode.getText().toString().trim();
        //if (authStr.equals(authInfo)) {
            showProgress(null);
            mPgService.register(headUrl, mobile, pwd, name, validateNum, recommandUserId).subscribe(new AbsAPICallback<ImFriendEntivity>() {
                @Override
                public void onNext(ImFriendEntivity friendEntivity) {
                    hideProgress();
                    showToast(getResources().getString(R.string.register_success));
                    userId = friendEntivity.getId() + "";
                    Intent intent = new Intent();
                    intent.putExtra("friendEntivity", friendEntivity);
                    setResult(LoginActivity.REGISTER, intent);

                    friendEntivity.setMobile(mobile);
                    ToolsUtils.saveUser(friendEntivity, mobileStr, phoneNumber.getText().toString().trim(), pwd);
                    new PrefsUtils(RegisterActivity.this, Constant.NEW_REGISTER).put(Constant.NEW_REGISTER, mobile);
                    scrollToFinishActivity();
                }

                @Override
                protected void onResultError(ApiException ex) {
                    hideProgress();
                    String exStr = ex.getDisplayMessage();
                    // 因为返回值 可能为 Json 数据,需要解析再展示
                    if (exStr.contains("{")) {
                        try {
                            String toastStr = new JSONObject(exStr).getString("data");
                            showToast(toastStr);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        showToast(ex.getDisplayMessage());
                    }
                }
            });
        }
   // }
    /**
     * 是否同意协议
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        isAgreeb = isChecked;

        //String authCodeString = authCode.getText().toString();
        if (isAgreeb) {
            register.setEnabled(true);
            register.setAlpha(1f);
        } else {
            register.setEnabled(false);
            register.setAlpha(0.5f);
        }
    }

    /**
     * 上传图片
     */
    public void uploadImage(final File photoFile) {
        showProgress(null);
        if (haveImg) {

            new Thread() {
                @Override
                public void run() {
                    super.run();
                   // String s = HttpAssist.uploadFile(photoFile,"",false);
                    String s = HttpAssist.uploadFile( ImageCompressUtil.changeImage(photoFile,RegisterActivity.this),"",false);
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        if (jsonObject.getString("code").equals("1")) {
                            String resultStr = jsonObject.getJSONObject("data").getJSONArray("info").getString(0);
                            Message msg = handler.obtainMessage();
                            msg.what = HEAD_IMG_URL;
                            Log.i(TAG, "run: --返回的头像路径--resultStr="+resultStr);
                            msg.obj = resultStr;
                            handler.sendMessage(msg);
                        } else {
                            hideProgress();
                            showToast(getResources().getString(R.string.net_visit_exception));
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } else {
            // 判断如果没有头像，直接注册
            Message msg = handler.obtainMessage();
            msg.what = HEAD_IMG_URL;
            msg.obj = "";
            handler.sendMessage(msg);
        }

    }

    /**
     * 展示确认推荐人的 Dialog
     */
    private void showAffirmDialog() {
        if (!ToolsUtils.currentNetState(this)) {
            return;
        }
        String trim = referrer.getText().toString().trim();
        mPgService.getUserById(trim, trim).subscribe(new AbsAPICallback<ImFriendEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                View layout = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.dialog_version_update_no, null);
                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(R.string.no_search_referrer_id);
                Dialog dialog1 = new Dialog(RegisterActivity.this, R.style.DialogStyle);
                dialog1.setContentView(layout);
                dialog1.show();
            }

            @Override
            public void onNext(ImFriendEntivity imFriendEntivity) {
                View dialog = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.recommend_data_affirm, null);
                TextView affirm = (TextView) dialog.findViewById(R.id.affirm);
                TextView referrerName = (TextView) dialog.findViewById(R.id.user_name);
                TextView referrerId = (TextView) dialog.findViewById(R.id.user_id);
                TextView referrerMobile = (TextView) dialog.findViewById(R.id.user_mobile);
                affirm.setOnClickListener(RegisterActivity.this);
                referrerName.setText(getString(R.string.referrer) + imFriendEntivity.getRealName());
                referrerId.setText(getString(R.string.referrer_id) + imFriendEntivity.getId() + "");
                referrerMobile.setText(getString(R.string.referrer_mobile) + imFriendEntivity.getMobile());
                affirmDialog = new AlertDialog.Builder(RegisterActivity.this).setView(dialog).create();
                affirmDialog.show();
                WindowManager.LayoutParams attributes = affirmDialog.getWindow().getAttributes();
                Display defaultDisplay = getWindowManager().getDefaultDisplay();
                attributes.width = ((int) (defaultDisplay.getWidth() * 0.85));
                affirmDialog.getWindow().setAttributes(attributes);
            }
        });
    }


    @TargetApi(23)
    protected void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            // 读写权限
            int pos = 0 ;
            if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//                permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n";
                pos ++ ;
            }
            if (addPermission(permissions, Manifest.permission.CAMERA)){
                pos ++ ;
            }
            if (2==pos){
                gotoPhoto();
            }
            if (permissions.size() > 0) {
                Log.i(TAG, "getPersimmions: --------001--------");
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        }else {
            gotoPhoto();
        }
    }

    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        Log.i(TAG, "getPersimmions: --------002--------");
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            Log.i(TAG, "getPersimmions: --------003--------");
            if (shouldShowRequestPermissionRationale(permission)) {
                //拒绝了也是可以强行提示的，不过如果再也不提示的情况还没处理
                permissionsList.add(permission);
                Log.i(TAG, "getPersimmions: --------004--------");
                return false;
            } else {
                Log.i(TAG, "getPersimmions: --------005--------");
                permissionsList.add(permission);
                return false;
            }
        } else {
            Log.i(TAG, "getPersimmions: --------0011--------");

            return true;
        }
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i(TAG, "getPersimmions: --------006--------");
        switch (requestCode) {
            case SDK_PERMISSION_REQUEST:
                Log.i(TAG, "getPersimmions: --------007--------");
                Map<String, Integer> perms = new HashMap<String, Integer>();
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                Log.i(TAG, "getPersimmions: --------008--------");
                try {
                    int isgre = 0 ;
                    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        isgre ++;
                    }
                    if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        isgre ++;
                    }
                    if (2 <= isgre){
                        gotoPhoto();
                    }else {
                        Toast.makeText(this, getResources().getString(R.string.disable_picture_permission_will_make_send_picture_unusable), Toast.LENGTH_SHORT)
                                .show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    boolean isgo = false ;
    public final  static  int CHOSE_IMG = 101 ;
    private void gotoPhoto(){
     //   if (!isgo){
            PhotoPickerIntent pt = new PhotoPickerIntent(RegisterActivity.this);
            pt.setSelectModel(SelectModel.MULTI);
            pt.setShowCarema(true); // 是否显示拍照， 默认false
            pt.setMaxTotal(1); // 最多选择照片数量，默认为9
            //pt.setImageConfig(conFig);
            startActivityForResult(pt,CHOSE_IMG);
      //  }
        isgo = true ;
    }

}
